package com.app.controller.miniprogram;

import com.app.common.Result;
import com.app.mapper.AdminUserMapper;
import com.app.model.AdminUserModel;
import com.app.model.ComplaintModel;
import com.app.model.UserModel;
import com.app.service.ComplaintService;
import com.app.service.UserService;
import com.app.util.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import javax.servlet.http.HttpServletRequest;

@Controller
@RestController
@RequestMapping("/complaint")
public class ComplaintController {
    @Autowired
    private ComplaintService complaintService;
    @Autowired
    private UserService userService;
    @Autowired
    private AdminUserMapper adminUserMapper;

    //我要投诉
    @PostMapping("/addComplaint")
    public Result<Object> addComplaint(@RequestBody ComplaintModel model, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        if (mobile == null) {
            return Result.error(405, "token过期");
        }
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        model.setUserId(user.getId());
        String s = complaintService.addComplaint(model);
        return Result.success(s);
    }

    //我的投诉列表
    @GetMapping("/myComplaint")
    public Result<Object> myComplaint(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        if (mobile == null) {
            return Result.error(405, "token过期");
        }
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(complaintService.complaintList(user.getId()));
    }

    //进度查询
    @GetMapping("/selectSchedule")
    public Result<Object> selectSchedule(String code, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        if (mobile == null) {
            return Result.error(405, "token过期");
        }
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        QueryWrapper<AdminUserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        AdminUserModel adminUserModel = adminUserMapper.selectOne(queryWrapper);
        if (adminUserModel != null) {
            return Result.success(complaintService.selectSchedule(code,adminUserModel.getId()));
        }
        return Result.success(complaintService.selectSchedule(code));
    }

    //查看详情
    @GetMapping("/selectDetail")
    public Result<Object> selectDetail(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        if (mobile == null) {
            return Result.error(405, "token过期");
        }
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        QueryWrapper<AdminUserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        AdminUserModel adminUserModel = adminUserMapper.selectOne(queryWrapper);
        if (adminUserModel != null) {
            return Result.success(complaintService.selectDetail(id,adminUserModel.getId()));
        }
        return Result.success(complaintService.selectDetail(id));
    }

    //办理
    @GetMapping("/examineComplaint")
    public Result<Object> examineComplaint(Integer id, Integer status, String remark, HttpServletRequest request, String handleRemark, String rejectRemark) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        if (mobile == null) {
            return Result.error(405, "token过期");
        }
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        QueryWrapper<AdminUserModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", user.getId());
        AdminUserModel adminUserModel = adminUserMapper.selectOne(queryWrapper);
        complaintService.examineComplaint(id, status, remark, handleRemark, rejectRemark, adminUserModel);
        return Result.success("操作成功");
    }

    //白名单
    @GetMapping("/whiteList")
    public Result<Object> whiteList() {
        return Result.success(complaintService.whiteList());
    }

    //投诉办理列表
    @GetMapping("/examineComplaintList")
    public Result<Object> examineComplaintList(Integer status, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.nothingness(token);
        if (mobile == null) {
            return Result.error(405, "token过期");
        }
        UserModel user = userService.getUserByOpenid(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        return Result.success(complaintService.examineComplaintList(status));
    }
}
